package com.apexsoft.monitor.front.console.magent.om;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.config.CommonConfig;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.monitor.front.console.host.om.Host;
import com.apexsoft.monitor.ssh.util.ShellUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class MonitorMagent2 {

    private static final Logger log = LogManager.getRootLogger();


    /**
     * 监控程序状态
     * 0 异常   1 正常
     */
    private String magentStauts = "0";


    /**
     * 监控程序状态信息
     */
    private String statusMsg = "";


    /**
     * 监控程序开启
     * @param host
     */
    public static JSONObject start(Host host){
        JSONObject result = new JSONObject();
        try {
            ShellUtils shellUtils = new ShellUtils(host);
            shellUtils.connect("");
            result = shellUtils.execCmd("ps -ef | grep magent");
            if(result.getInteger("code") == -1){
                return result;
            }
            String[] array = result.getString("note").split("\\n");
            boolean isService = isStartService(host,array);
            //存在,什么都不做,不存在启动服务
            if(!isService){
                shellUtils.putFile(host.getHostDir());
                shellUtils.execCmd("chmod a+x "+host.getHostDir()+"/magent");
                //mAgent 9009 amqp://admin:apexsoft123@192.168.3.134:5672/
                //拼接队列地址
                String mqHostIp = CommonConfig.getString("MQ.HOST");
                String mqPort = CommonConfig.getString("MQ.PORT");
                String mqUserName = CommonConfig.getString("MQ.USERNAME");
                String mqPassword = CommonConfig.getString("MQ.PASSWORD");
                String mqUrl = " amqp://"+mqUserName+":"+mqPassword+"@"+mqHostIp+":"+mqPort+"/";
                result = shellUtils.execCmd("nohup "+host.getHostDir()+"/magent "+host.getAgentPort()+mqUrl+" > /dev/null &");
            }
            result.put(Result.NOTE,"监控程序运行正常!("+host.getHostIp()+":"+host.getAgentPort()+")");
            shellUtils.closeSession();
            return result;
        }catch (Exception e){
            result.put("magentStatus","0");
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 监控程序关闭
     * @param host
     */
    public static void shutDown(Host host){

    }


    /**
     * 主机上是否存在监控程序
     * @param host
     * @param array
     * @return
     */
    public static boolean isStartService(Host host,String[] array){
        boolean isStartService = false;
        try{
            for(String val :array){
                if(val.indexOf(host.getHostDir())!=-1
                        && val.indexOf(String.valueOf(host.getAgentPort()))!=-1){
                    isStartService = true;
                    break;
                }
            }
        }catch(Exception e){
            log.error(e);
            isStartService = false;
        }
        return isStartService;
    }

    public static boolean isHasHeartbeat(Host host){
        boolean result = false;


        return result;
    }
}
